from PyQt5.QtWidgets import QDialog
from UI.control import Ui_Control
from driver import DriverStatus
from PyPhysLeo.data_core import Vec3F

class ControlWidget(QDialog):

    def __init__(self, driver, parent = None):
        super(ControlWidget, self).__init__(parent)
        self.driver = driver
        self.ui = Ui_Control()
        self.ui.setupUi(self)
        self.ui.start_stop_button.clicked.connect(self.slotStartStop)
        self.ui.gravity_x.editingFinished.connect(self.slotGravityChange)
        self.ui.gravity_y.editingFinished.connect(self.slotGravityChange)
        self.ui.gravity_z.editingFinished.connect(self.slotGravityChange)
        self.ui.velocity_damping.editingFinished.connect(self.slotDampingChange)
        
    def closeEvent(self, event):
        self.driver.status = DriverStatus.FINISHED

    def slotStartStop(self):
        if self.driver.status == DriverStatus.RUNNABLE:
            self.driver.status = DriverStatus.STOPPED
        elif self.driver.status == DriverStatus.STOPPED:
            self.driver.status = DriverStatus.RUNNABLE

    def slotGravityChange(self):
        x = self.ui.gravity_x.value()
        y = self.ui.gravity_y.value()
        z = self.ui.gravity_z.value()
        for gravity in self.gravities_:
            gravity.setGravityAcceleration(Vec3F(x,y,z))

    def slotDampingChange(self):
        d = self.ui.velocity_damping.value()
        for damping in self.dampings_:
            damping.setVelocityDampingParam(d)


